package com.edu.user.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.edu.user.domain.dto.DoctorScheduleDTO;
import com.edu.user.domain.dto.DoctorUpdateDTO;
import com.edu.user.domain.dto.DoctorUpdateInfoDTO;
import com.edu.user.domain.dto.UserUpdateDTO;
import com.edu.user.domain.po.Doctor;
import com.edu.user.domain.po.DoctorSchedule;
import com.edu.user.domain.po.User;
import com.edu.user.domain.vo.DoctorInfoVO;
import com.edu.user.domain.vo.DoctorScheduleVO;
import com.edu.user.domain.vo.DoctorVO;
import com.edu.user.service.DoctorScheduleService;
import com.edu.user.service.DoctorService;
import com.edu.user.utils.Result;
import com.edu.user.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.JsonAggregateUniqueKeysType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/doctors")
public class DoctorController {

    private final DoctorService doctorService;
    private final DoctorScheduleService doctorScheduleService;

    @Autowired
    public DoctorController(DoctorService doctorService, DoctorScheduleService doctorScheduleService) {
        this.doctorService = doctorService;
        this.doctorScheduleService = doctorScheduleService;
    }

    @GetMapping("/all")
    public Result getAllDoctors() {
        log.info("getAllDoctors: 尝试获取所有医生信息");
        List<DoctorVO> doctors = doctorService.getAll();
        log.info("getAllDoctors: 成功获取所有医生信息，共计 {} 条记录", doctors.size());
        return Result.success(doctors);
    }

    @GetMapping("/{doctorId}")
    public Result getDoctor(@PathVariable Integer doctorId) {
        log.info("getDoctor: 尝试获取医生ID为 {} 的详细信息", doctorId);
        DoctorVO doctor = doctorService.getDetailById(doctorId);
        if (doctor == null) {
            log.warn("getDoctor: 医生ID为 {} 的信息不存在", doctorId);
            return Result.error("医生不存在");
        }
        log.info("getDoctor: 成功获取医生ID为 {} 的详细信息", doctorId);
        return Result.success(doctor);
    }

    // 患者获取某个医生的日程列表
    @GetMapping("/schedules/patient/{doctorId}")
    public Result getDoctorSchedulesById(@PathVariable Integer doctorId) {
        log.info("getDoctorSchedules: 尝试获取医生id：{}的日程表",doctorId);

        return doctorScheduleService.selectById(Math.toIntExact(doctorId));
    }
    // 医生获取某个医生的日程列表
    @GetMapping("/schedules/doctor")
    public Result getDoctorSchedules() {
        User user = ThreadLocalUtil.get();
        log.info("getDoctorSchedules: 尝试获取医生的日程表");
        if (user==null){
            log.warn("deleteDoctorSchedules: 未登录的用户尝试医生的日程的记录");
            return Result.error("请先登录");
        }
        Long doctorId = user.getId();
        return doctorScheduleService.selectById(Math.toIntExact(doctorId));
    }

    @PostMapping("/schedules")
    public Result addDoctorSchedules(@RequestBody DoctorScheduleDTO doctorScheduleDTO){
        log.info("addDoctorSchedules: 尝试添加医生的日程");
        User user=ThreadLocalUtil.get();
        Long id = user.getId();
        doctorScheduleDTO.setDoctorId(id);
        return doctorScheduleService.addDoctorSchedule(doctorScheduleDTO);
    }

    @DeleteMapping("/schedules")
    public Result deleteDoctorSchedules( @RequestParam Integer scheduleId) {
        User user = ThreadLocalUtil.get();
        if (user == null) {
            log.warn("deleteDoctorSchedules: 未登录的用户尝试删除医生的日程ID为 {} 的记录",scheduleId);
            return Result.error("请先登录");
        }
        Long doctorId = user.getId();
        log.info("deleteDoctorSchedules: 用户ID为 {} 尝试删除医生ID为 {} 的日程ID为 {} 的记录", user.getId(), doctorId, scheduleId);
        boolean isok = doctorScheduleService.removeByscheduleId(scheduleId);
        if (isok) {
            log.info("deleteDoctorSchedules: 成功删除医生ID为 {} 的日程ID为 {} 的记录", doctorId, scheduleId);
            return Result.success("删除成功");
        } else {
            log.error("deleteDoctorSchedules: 删除医生ID为 {} 的日程ID为 {} 的记录失败", doctorId, scheduleId);
            return Result.error("删除失败");
        }
    }
    @PutMapping("/schedules")
    public Result updateDoctorSchedules(@RequestBody DoctorUpdateInfoDTO updateDTO) {
        System.out.println(updateDTO);
        User user = ThreadLocalUtil.get();
        if (user == null) {
            log.warn("updateDoctorSchedule: 未登录的用户尝试修改医生的日程ID为 {} 的记录",updateDTO.getScheduleId());
            return Result.error("请先登录");
        }
        Long doctorId = user.getId();
        updateDTO.setDoctorId(doctorId);
        log.info("updateDoctorSchedules: 尝试更新医生ID为 {} 的排班", updateDTO.getDoctorId());
        return doctorScheduleService.updateDoctorSchedule(updateDTO);
    }


    @GetMapping("/department")
    public Result getDoctorsByDepartment(@RequestParam String departname) {
        log.info("getDoctorsByDepartment: 尝试获取科室名称为 {} 的医生信息", departname);
        List<DoctorVO> doctors = doctorService.getDoctorsByDepartment(departname);
        log.info("getDoctorsByDepartment: 成功获取科室名称为 {} 的医生信息，共计 {} 条记录", departname, doctors.size());
        System.out.println(doctors);
        return Result.success(doctors);
    }
    // 根据医生姓名模糊查询
    @GetMapping("/search")
    public Result getDoctorsByName(@RequestParam String name) {
        log.info("getDoctorsByName: 尝试模糊查询姓名包含 {} 的医生信息", name);
        List<DoctorVO> doctors = doctorService.getDoctorsByName(name);
        log.info("getDoctorsByName: 成功模糊查询姓名包含 {} 的医生信息，共计 {} 条记录", name, doctors.size());
        return Result.success(doctors);
    }

    /**
     * 医生查询个人信息
     * @return Result
     */
    @GetMapping("/doctorInfo")
    public Result selectDoctorInfo() {
        log.info("selectDoctorInfo:  医生尝试获取个人的详细信息");
        User user = ThreadLocalUtil.get();
        if (user == null) {
            log.warn("select: 未登录的医生尝试获取个人信息");
            return Result.error("请先登录");
        }
        Long userId = user.getId();
        log.info("select: 获取为医生 {} 的信息", userId);
        DoctorInfoVO doctorInfoVO = doctorService.getDoctorInfoById(userId);
        if (doctorInfoVO != null) {
            log.info("select: 成功获取ID为 {} 的医生个人信息", userId);
            return Result.success(doctorInfoVO);
        }
        log.warn("select: 医生ID为 {} 的信息不存在", userId);
        return Result.error("医生不存在");
    }
    /**
     *更新医生信息
     * @param
     * @return Result<String>
     */
    @PutMapping("/update")
    public Result<String> updateProfile(@RequestBody DoctorUpdateDTO updatedDoctor){
        User user=ThreadLocalUtil.get();
        if(user==null){
            log.warn("select: 未登录的医生尝试更改个人信息");
            return Result.error("请先登录");
        }
        log.info("updateProfile: 医生ID为 {} 尝试更新信息", user.getId());
        return doctorService.updateDoctorProfile(updatedDoctor,user.getId());
    }
//      log.info("getDoctor: 尝试获取医生ID为 {} 的详细信息", doctorId);
//    DoctorVO doctor = doctorService.getDetailById(doctorId);
//        if (doctor == null) {
//        log.warn("getDoctor: 医生ID为 {} 的信息不存在", doctorId);
//        return Result.error("医生不存在");
//    }
//        log.info("getDoctor: 成功获取医生ID为 {} 的详细信息", doctorId);
//        return Result.success(doctor);
}
